package repository

import (
	"golang.org/x/net/context"
	"internal/internal/domain"
	"internal/internal/repository/dao"
)

var (
	ErrUserDuplicateEmail = dao.ErrUserDuplicateEmail
	ErrUserNotFound       = dao.ErrUserNotFound
)

type UserRepository struct {
	dao *dao.UserDAO
}

func NewUserRepository(d *dao.UserDAO) *UserRepository {
	return &UserRepository{
		dao: d,
	}
}

func (ur *UserRepository) Create(ctx context.Context, u domain.User) error {
	err := ur.dao.Insert(ctx, dao.User{
		Username: u.Username,
		Password: u.Password,
	})
	return err
}

func (ur *UserRepository) FindByName(ctx context.Context, username string) (domain.User, error) {
	u, err := ur.dao.FindByName(ctx, username)
	if err != nil {
		return domain.User{}, err
	}
	return domain.User{
		Id:       u.Id,
		Username: u.Username,
		Password: u.Password,
	}, nil
}
